import 'package:flutter/animation.dart';
import 'package:get/get.dart';
import 'package:pet_app/base/abstract_network.dart';
import 'package:pet_app/base/base_controller.dart';
import 'package:pet_app/model/breed.dart';
import 'package:pet_app/net/http.dart';
import 'package:pet_app/widget/loading_widget.dart';

class CardSwiperLogic extends BaseController
    with GetTickerProviderStateMixin, AbstractNetWork {
  List<Breed> breedList = [];
  late int breedGroupId;
  int preIndex = 0, index = 0;
  late AnimationController fadeInController;
  late AnimationController fadeOutController;
  late Animation<double> fadeInAnimation;
  late Animation<double> fadeOutAnimation;
  int durationMilliseconds = 1300;

  @override
  void initData() {
    getNetworkData(configNetworkParams());
    fadeInController = AnimationController(
        vsync: this, duration: Duration(milliseconds: durationMilliseconds));
    fadeOutController = AnimationController(
        vsync: this, duration: Duration(milliseconds: durationMilliseconds));
    fadeInAnimation = Tween<double>(begin: 0, end: 1).animate(fadeInController);
    fadeOutAnimation =
        Tween<double>(begin: 1, end: 0).animate(fadeOutController);
  }

  @override
  void onClose() {
    fadeInController.dispose();
    fadeOutController.dispose();
    super.dispose();
  }

  bool isToggled = true;

  void changeIndex(int index) {
    if (isToggled) {
      this.index = index;
      update(['background']);
      fadeInController.forward();
      fadeOutController.forward();
    } else {
      preIndex = index;
      update(['background']);
      fadeInController.reverse();
      fadeOutController.reverse();
    }
    isToggled = !isToggled;
  }

  @override
  configNetworkParams() {
    breedGroupId = Get.arguments['breedGroupId'];
  }

  @override
  void getNetworkData(params) {
    Loading.show();
    Http().client.listByBreedGroupId(breedGroupId).then((value) {
      breedList = value.data ?? [];
      Loading.dismiss();
      update();
    });
  }
}
